typedef struct queue Queue;
struct queue {
	int jobnum;
	int time;
	int running_time;
	int priority;
	int q_priority;		//scheduling based priority.
	Queue* next;
};

Queue* head;
Queue* rear;
void q_init() {
	head = NULL;
	rear = NULL;
}

Queue* create_node(int jobnum, int time, int running_time, int priority, int q_priority) {
	Queue* node = (Queue *)malloc(sizeof(Queue));
	if(node == NULL) {
		return NULL;
	}
	node->jobnum = jobnum;
	node->time = time;
	node->running_time = running_time;
	node->priority = priority;
	node->q_priority = q_priority;
	node->next = NULL;
	return node;
}

void add_node (Queue* node) {
	if(not_init) {
		q_init();
		not_init = 0;
	}

	if(head == NULL) {	//queue is empty;
		head = rear = node;
		return;
	} 
	if(node->q_priority < head->q_priority) {  //greatest priority
		node->next = head;
		head = node;
		return;
	}
	if(node->q_priority > rear->q_priority) {  //least priority
		rear->next = node;
		rear = node;
		return;
	}
	Queue* temp = head;
	Queue* prev = head;
	while ( (node->q_priority < temp->q_priority) && temp != NULL ) { //assuming lower number has higher priority.
		prev = temp;
		temp = temp->next;
	}
	prev->next = node;
	node->next = temp;
	return;
}

int remove_node () {
	if(head == NULL) {
		return 0;
	}
	Queue* temp = head;
	head = head->next;
	free(temp);
	return 1;
}

void show_queue() {
    // Display entire process queue

	Queue* temp = head;
	while(temp) {
		printf("Job Num:%10d", temp->jobnum);
		temp = temp->next;
	}
}

